package com.lz.repository.ly.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lz.entity.AuditLogging;
import com.lz.entity.EquipmentSheet;
import com.lz.entity.InstallationPerson;
import com.lz.entity.UpholsteryType;
import com.lz.repository.ly.EquipmentSheetByStateRepository;
import com.lz.vo.ly.EquipmentSheetConditionVO;

@Repository
public class EquipmentSheetByStateRepositoryImpl implements EquipmentSheetByStateRepository {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<EquipmentSheetConditionVO> getEquipmentSheetByState(Integer pageNo,Integer pageSize,EquipmentSheet sheet,String start_time,String end_time) {
		StringBuffer hql = new StringBuffer("select new com.lz.vo.ly.EquipmentSheetConditionVO(es,ca,cm,ut) from EquipmentSheet es,Car ca,Customer cm,UpholsteryType ut  "
				+ " where es.car.carId=ca.carId and es.customer.customerId=cm.customerId and es.upholsteryType.ecorationNo=ut.ecorationNo");
		
		if(sheet.getEquipmentSheetNo() != null) {
			hql.append(" and es.equipmentSheetNo like '%"+sheet.getEquipmentSheetNo()+"%'");
		}
		if(sheet.getCustomer() != null && !"".equals(sheet.getCustomer().getCustomerName())) {
			hql.append(" and cm.customerName like '%"+sheet.getCustomer().getCustomerName()+"%'");
		}
		if(sheet.getCar() != null && !"".equals(sheet.getCar().getVin())) {
			hql.append(" and ca.vin like '%"+sheet.getCar().getVin()+"%'");
		}
		if(sheet.getCar() != null && !"".equals(sheet.getCar().getCarCard())) {
			hql.append(" and ca.carCard like '%"+sheet.getCar().getCarCard()+"%'");
		}
		if(sheet.getUpholsteryType() != null && !"-1".equals(sheet.getUpholsteryType().getUpholsteryType()) ) {
			hql.append(" and ut.upholsteryType ='"+sheet.getUpholsteryType().getUpholsteryType()+"'");
		}
		if(start_time != null && !"".equals(start_time)) {
			hql.append(" and es.openingTime >= '"+start_time+"'" );
		}
		if(end_time != null && !"".equals(end_time)) {
			hql.append(" and es.openingTime <= '"+end_time+"'");
		}
		Query query = getSession().createQuery(hql.toString());
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		return query.list();
	}

	@Override
	public Integer getEsByState(EquipmentSheet sheet,String start_time,String end_time) {
		
		StringBuffer hql = new StringBuffer("select count(1) from EquipmentSheet es,Car ca,Customer cm,UpholsteryType ut  "
				+ " where es.car.carId=ca.carId and es.customer.customerId=cm.customerId and es.upholsteryType.ecorationNo=ut.ecorationNo");
		
		if(sheet.getEquipmentSheetNo() != null) {
			hql.append(" and es.equipmentSheetNo like '%"+sheet.getEquipmentSheetNo()+"%'");
		}
		if(sheet.getCustomer() != null && !"".equals(sheet.getCustomer().getCustomerName())) {
			hql.append(" and cm.customerName like '%"+sheet.getCustomer().getCustomerName()+"%'");
		}
		if(sheet.getCar() != null && !"".equals(sheet.getCar().getVin())) {
			hql.append(" and ca.vin like '%"+sheet.getCar().getVin()+"%'");
		}
		if(sheet.getCar() != null && !"".equals(sheet.getCar().getCarCard())) {
			hql.append(" and ca.carCard like '%"+sheet.getCar().getCarCard()+"%'");
		}
		if(sheet.getUpholsteryType() != null && !"-1".equals(sheet.getUpholsteryType().getUpholsteryType()) ) {
			hql.append(" and ut.upholsteryType ='"+sheet.getUpholsteryType().getUpholsteryType()+"'");
		}
		if(start_time != null && !"".equals(start_time)) {
			hql.append(" and es.openingTime >= '"+start_time+"'" );
		}
		if(end_time != null && !"".equals(end_time)) {
			hql.append(" and es.openingTime <= '"+end_time+"'");
		}
		
		Query query = getSession().createQuery(hql.toString());
		Object result = query.uniqueResult();
		
		return ((Long)result).intValue();
	}

	@Override
	public Integer updEquipmentSheetState(String result,Integer equipmentSheetNo) {
		String hql = "update EquipmentSheet es set es.equipmentSheetState=:var1 where es.equipmentSheetNo=:var2";
		Query query = getSession().createQuery(hql);
		query.setString("var1", result);
		query.setInteger("var2", equipmentSheetNo);
		int i = query.executeUpdate();
		return i;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UpholsteryType> getType() {
		String hql = "from UpholsteryType";
		Query query = getSession().createQuery(hql);
		List<UpholsteryType> list = query.list();
		
		return list;
	}

	@Override
	public void savaAuditLogging(AuditLogging logging) {
		getSession().save(logging);
	}

	@Override
	public EquipmentSheet getEquipmentSheetById(Integer id) {
		EquipmentSheet es = (EquipmentSheet) getSession().get(EquipmentSheet.class, id);
		if(es != null) {
			return es;
		}
		return null;
	}

	//添加安装人员
	@Override
	public void addPerson(Integer id,Integer personNo) {
		
		EquipmentSheet es = (EquipmentSheet) getSession().get(EquipmentSheet.class, id);
		InstallationPerson person = (InstallationPerson) getSession().get(InstallationPerson.class, personNo);
		if(es != null && person != null) {
			es.setNstallationPerson(person);
		}
		getSession().flush();
		
	}

}
